﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Excel;
using GoldPrinter;
using cfg = System.Configuration;
namespace BYManageInfoSystem
{
    public enum ViewDay
    {
        Yesterday = 0xc00,
        Today = 0xc01,
        Tomorrow = 0xc02,
        Allday = 0xc03
    }
    public partial class Frm_DispatchCarSheet_New : DockContent
    {
        private string m_strFolder = "d:/派车单/";
        private ViewDay m_vd = new ViewDay();
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private bool m_bInsertFlag = false;
        private bool m_bCopyInsertFlag = false;
        private int m_nDelIndex = -1;
        private DGBY.BLL.PaiChe_FacTab m_bllPaiChe_FacTab = new DGBY.BLL.PaiChe_FacTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.PaiCheTabNew m_bllPaiCheTabNew = new DGBY.BLL.PaiCheTabNew();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.TCCompDataTab m_bllTCCompDataTab = new DGBY.BLL.TCCompDataTab();
        private DGBY.BLL.FacAddrTab m_bllFacAddrTab = new DGBY.BLL.FacAddrTab();
        private DGBY.BLL.FacConPeoAndTelTab m_bllFacConPeoAndTelTab = new DGBY.BLL.FacConPeoAndTelTab();
        private DGBY.BLL.FeeDLYSTruePayTab m_bllFeeDLYSTruePayTab = new DGBY.BLL.FeeDLYSTruePayTab();
        private DGBY.BLL.BGPackingTab m_bllBGPackingTab = new DGBY.BLL.BGPackingTab();

     
        private DGBY.BLL.TCCompConPeoTelTab m_bllTCCompConPeoTelTab = new DGBY.BLL.TCCompConPeoTelTab();
        private DGBY.BLL.GuiXingTab m_bllGuiXingTab = new DGBY.BLL.GuiXingTab();
        private DGBY.BLL.PortTab m_bllPortTab = new DGBY.BLL.PortTab();
        private DGBY.Model.PaiCheTabNew m_modelPaiCheTabNew = new DGBY.Model.PaiCheTabNew();

        public Frm_DispatchCarSheet_New()
        {
            InitializeComponent();
        }
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }
        private void Frm_DispatchCarSheet_New_Load(object sender, EventArgs e)
        {
            InitUltraWinGridSetting();
            InitComBox();
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            if (string.IsNullOrEmpty(strWhere))
            {
                if (m_vd == ViewDay.Today)
                    strWhere = "PaiCheTabNew.ZGDate = '" + System.DateTime.Now.Date + "'";
                else if (m_vd == ViewDay.Allday)
                {
                    string strTemp = this.comboBoxShowWay.Text;
                    if (strTemp == "全部记录")
                        strWhere = "";
                    else
                        strWhere = "PaiCheTabNew.ZGDate >= '" + System.DateTime.Now.Date.AddDays(-180) + "'";
                }
                else if (m_vd == ViewDay.Yesterday)
                    strWhere = "PaiCheTabNew.ZGDate = '" + System.DateTime.Now.Date.AddDays(-1) + "'";
                else if (m_vd == ViewDay.Tomorrow)
                    strWhere = "PaiCheTabNew.ZGDate = '" + System.DateTime.Now.Date.AddDays(1) + "'";
            }

            ultraGrid1.DataSource = null;
            DataSet ds = m_bllPaiCheTabNew.GetList(strWhere);


            DataSet dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;



            dsTemp = m_bllFacAddrTab.GetList("");
            this.ultraDropDownFacAddr.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFacAddr.DisplayMember = "FacAddr";
            this.ultraDropDownFacAddr.ValueMember = "FacAddrTabID";
            this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns["FacAddr"].Hidden = false;

            dsTemp = m_bllFacConPeoAndTelTab.GetList("");

            this.ultraDropDownFacCeo.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFacCeo.DisplayMember = "FacConPeoName";
            this.ultraDropDownFacCeo.ValueMember = "FacConPeoAndTelTabID";
            this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns["FacConPeoName"].Hidden = false;

            dsTemp = m_bllTCCompDataTab.GetList("");

            this.ultraDropDownTuoCheComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownTuoCheComp.DisplayMember = "拖车公司简称";
            this.ultraDropDownTuoCheComp.ValueMember = "TCCompDataTabID";
            this.ultraDropDownTuoCheComp.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownTuoCheComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownTuoCheComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownTuoCheComp.DisplayLayout.Bands[0].Columns["拖车公司简称"].Hidden = false;

            dsTemp = m_bllTCCompConPeoTelTab.GetList("");

            this.ultraDropDownTuoCheCeo.DataSource = dsTemp.Tables[0];
            this.ultraDropDownTuoCheCeo.DisplayMember = "TCCompConPeoName";
            this.ultraDropDownTuoCheCeo.ValueMember = "TCCompConPeoTelTabID";
            this.ultraDropDownTuoCheCeo.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownTuoCheCeo.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownTuoCheCeo.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownTuoCheCeo.DisplayLayout.Bands[0].Columns["TCCompConPeoName"].Hidden = false;

            dsTemp = m_bllGuiXingTab.GetList("");

            this.ultraDropDownGuixing.DataSource = dsTemp.Tables[0];
            this.ultraDropDownGuixing.DisplayMember = "柜型";
            this.ultraDropDownGuixing.ValueMember = "GuiXingTabID";
            this.ultraDropDownGuixing.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownGuixing.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownGuixing.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownGuixing.DisplayLayout.Bands[0].Columns["柜型"].Hidden = false;

            dsTemp = m_bllPortTab.GetList("");

            this.ultraDropDownPort.DataSource = dsTemp.Tables[0];
            this.ultraDropDownPort.DisplayMember = "港口名称";
            this.ultraDropDownPort.ValueMember = "PortTabID";
            this.ultraDropDownPort.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownPort.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownPort.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownPort.DisplayLayout.Bands[0].Columns["港口名称"].Hidden = false;

            dsTemp = m_bllT_ACL_User.GetList("");
            
            this.ultraDropDownUser.DataSource = dsTemp.Tables[0];
            this.ultraDropDownUser.DisplayMember = "用户名称";
            this.ultraDropDownUser.ValueMember = "UserPasswordTabID";
            this.ultraDropDownUser.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownUser.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownUser.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownUser.DisplayLayout.Bands[0].Columns["用户名称"].Hidden = false;

            
            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司电话"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司传真"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂电话"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂传真"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["FacConPeoAndTelTabID"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["TCCompDataTabID"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["ZhuangGuiSiteTabID"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["UserPasswordTabID2"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["FactoryDataTabID"].Hidden = true;
            

            this.ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂信息"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["生成运输费"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].ValueList = this.ultraDropDownFac;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂地址"].ValueList = this.ultraDropDownFacAddr;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂联系人"].ValueList = this.ultraDropDownFacCeo;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司名称"].ValueList = this.ultraDropDownTuoCheComp;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司联系人"].ValueList = this.ultraDropDownTuoCheCeo;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["柜型"].ValueList = this.ultraDropDownGuixing;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["交柜地点"].ValueList = this.ultraDropDownPort;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["业务员"].ValueList = this.ultraDropDownUser;

        }


        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["单据号"].Value = MakeSJPackDJNo();
            CurrentUGR.Cells["经营单位联系人"].Value = Frm_Main.m_strLoginUser;

            CurrentUGR.Cells["拖车公司名称"].Value = 1;
            CurrentUGR.Cells["拖车公司联系人"].Value = 7;
            //CurrentUGR.Cells["拖车公司电话"].Value = "";
            //CurrentUGR.Cells["拖车公司传真"].Value = "";

            CurrentUGR.Cells["订舱号"].Value = "";
            CurrentUGR.Cells["装柜日期"].Value = System.DateTime.Now.Date;
            CurrentUGR.Cells["装柜时"].Value = 0;
            CurrentUGR.Cells["装柜分"].Value = 0;
                    
            CurrentUGR.Cells["柜型"].Value = 1;
            CurrentUGR.Cells["交柜地点"].Value = 1;
            CurrentUGR.Cells["业务员"].Value = 49;
            CurrentUGR.Cells["工厂联系人"].Value = 1;
            CurrentUGR.Cells["工厂名称"].Value = 1;
            CurrentUGR.Cells["工厂地址"].Value = 1;
            //CurrentUGR.Cells["工厂电话"].Value = "";
            //CurrentUGR.Cells["工厂传真"].Value = "";
            CurrentUGR.Cells["工厂联系人电话"].Value = "";

            CurrentUGR.Cells["柜号"].Value = "";
            CurrentUGR.Cells["封条号"].Value = "";
            CurrentUGR.Cells["到厂小时"].Value = 0;
            CurrentUGR.Cells["到厂分钟"].Value = 0;

            CurrentUGR.Cells["过磅重量"].Value = 0;

            CurrentUGR.Cells["特殊事项"].Value = "";
            CurrentUGR.Cells["备注"].Value = "";
            CurrentUGR.Cells["装柜时长"].Value = 0;
            CurrentUGR.Cells["临时上柜"].Value = false;
            CurrentUGR.Cells["有堆存费"].Value = 0;
            CurrentUGR.Cells["有异提费"].Value = 0;
            CurrentUGR.Cells["有转关费"].Value = 0;
            CurrentUGR.Cells["有运输费"].Value = 0;
            //if (m_bInsertFlag)
            //{
            //    ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
            //    m_bInsertFlag = false;
            //    AddGoodsDetail(nIndex);

            //}
        }
        private bool AddGoodsDetailCopy(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.PaiCheTabNew modelPaiCheTabNew = new DGBY.Model.PaiCheTabNew();
                modelPaiCheTabNew.PaiCheDJNo = Convert.ToInt64(CurrentUGR.Cells["单据号"].Value.ToString(), 10);

                modelPaiCheTabNew.PaiCheDJDate = m_modelPaiCheTabNew.PaiCheDJDate;
                modelPaiCheTabNew.UserPasswordTabID = Frm_Main.m_nLoginUserId;
                modelPaiCheTabNew.TCCompConPeoTelTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司联系人"].Tag.ToString(), 10);
                modelPaiCheTabNew.TCCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司名称"].Value.ToString(), 10);

                modelPaiCheTabNew.DingCangNo = CurrentUGR.Cells["订舱号"].Value.ToString();
                modelPaiCheTabNew.ZGDate = (DateTime)CurrentUGR.Cells["装柜日期"].Value;
                modelPaiCheTabNew.ZGHour = Convert.ToInt32(CurrentUGR.Cells["装柜时"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGMin = Convert.ToInt32(CurrentUGR.Cells["装柜分"].Value.ToString(), 10);

                modelPaiCheTabNew.GuiXingTabID = Convert.ToInt32(CurrentUGR.Cells["柜型"].Value.ToString(), 10);
                modelPaiCheTabNew.PortTabID = Convert.ToInt32(CurrentUGR.Cells["交柜地点"].Value.ToString(), 10);
                modelPaiCheTabNew.FacAddrTabID = Convert.ToInt32(CurrentUGR.Cells["工厂地址"].Tag.ToString(), 10);
                modelPaiCheTabNew.FacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["工厂联系人"].Tag.ToString(), 10);
                modelPaiCheTabNew.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂名称"].Value.ToString(), 10);
                modelPaiCheTabNew.UserPasswordTabID2 = Convert.ToInt32(CurrentUGR.Cells["业务员"].Value.ToString(), 10);


                modelPaiCheTabNew.GuiNo = CurrentUGR.Cells["柜号"].Value.ToString();
                modelPaiCheTabNew.FengTiaoNo = CurrentUGR.Cells["封条号"].Value.ToString();
                modelPaiCheTabNew.ArriveFacHour = Convert.ToInt32(CurrentUGR.Cells["到厂小时"].Value.ToString(), 10);
                modelPaiCheTabNew.ArriveFacMin = Convert.ToInt32(CurrentUGR.Cells["到厂分钟"].Value.ToString(), 10);
                modelPaiCheTabNew.GuoBangWeight = Convert.ToInt32(CurrentUGR.Cells["过磅重量"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGHourCount = Convert.ToInt32(CurrentUGR.Cells["装柜时长"].Value.ToString(), 10);
                modelPaiCheTabNew.LinShiShangGui = Convert.ToBoolean(CurrentUGR.Cells["临时上柜"].Value.ToString());
                modelPaiCheTabNew.SpecialNote = CurrentUGR.Cells["特殊事项"].Value.ToString();
                modelPaiCheTabNew.Note = CurrentUGR.Cells["备注"].Value.ToString();


                modelPaiCheTabNew.IsGaidanFee = Convert.ToBoolean(CurrentUGR.Cells["有堆存费"].Value.ToString());
                modelPaiCheTabNew.IsYiTiFee = Convert.ToBoolean(CurrentUGR.Cells["有异提费"].Value.ToString());
                modelPaiCheTabNew.IsZhuanGuanFee = Convert.ToBoolean(CurrentUGR.Cells["有转关费"].Value.ToString());
                modelPaiCheTabNew.bitFiledReserv = Convert.ToBoolean(CurrentUGR.Cells["有运输费"].Value.ToString());
                modelPaiCheTabNew.stringFiledReserv1 = "";
                modelPaiCheTabNew.stringFiledReserv2 = "";
                modelPaiCheTabNew.intFiledReserv1 = 0;
                modelPaiCheTabNew.intFiledReserv2 = 0;
                modelPaiCheTabNew.datetimeFiledReserv1 = System.DateTime.Now.Date;
                modelPaiCheTabNew.datetimeFiledReserv2 = System.DateTime.Now.Date;
                modelPaiCheTabNew.bitFiledReserv = false;
                m_bllPaiCheTabNew.Add(modelPaiCheTabNew);
                //if (modelPaiCheTabNew.ArriveFacHour == 0)
                //    CurrentUGR.Cells["到厂小时"].Value = null;
                //if (modelPaiCheTabNew.ArriveFacMin == 0)
                //    CurrentUGR.Cells["到厂分钟"].Value = null;
                //if (modelPaiCheTabNew.GuoBangWeight == 0)
                //    CurrentUGR.Cells["过磅重量"].Value = null;
                //if (modelPaiCheTabNew.ZGHourCount == 0)
                //    CurrentUGR.Cells["装柜时长"].Value = null;

                Frm_Main.Log2DB(this.Text, "增加", modelPaiCheTabNew.PaiCheDJNo.ToString());

                if (!m_bCopyInsertFlag)
                {
                    ShowAllRecordInDataGridView("");
                }
                else
                {
                    ShowAllRecordInDataGridView("PaiCheDJNo = '" + modelPaiCheTabNew.PaiCheDJNo + "'");
                }

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValueCopyContract(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["单据号"].Value = m_modelPaiCheTabNew.PaiCheDJNo;
            CurrentUGR.Cells["经营单位联系人"].Value = Frm_Main.m_strLoginUser;

            CurrentUGR.Cells["拖车公司名称"].Value = m_modelPaiCheTabNew.TCCompDataTabID;
            CurrentUGR.Cells["拖车公司联系人"].Value = m_modelPaiCheTabNew.TCCompConPeoTelTabID;
            //CurrentUGR.Cells["拖车公司电话"].Value = "";
            //CurrentUGR.Cells["拖车公司传真"].Value = "";

            CurrentUGR.Cells["订舱号"].Value = m_modelPaiCheTabNew.DingCangNo;
            CurrentUGR.Cells["装柜日期"].Value = m_modelPaiCheTabNew.ZGDate;
            CurrentUGR.Cells["装柜时"].Value = m_modelPaiCheTabNew.ZGHour;
            CurrentUGR.Cells["装柜分"].Value = m_modelPaiCheTabNew.ZGMin;

            CurrentUGR.Cells["柜型"].Value = m_modelPaiCheTabNew.GuiXingTabID;
            CurrentUGR.Cells["交柜地点"].Value = m_modelPaiCheTabNew.PortTabID;
            CurrentUGR.Cells["业务员"].Value = m_modelPaiCheTabNew.UserPasswordTabID2;
            CurrentUGR.Cells["工厂联系人"].Value = m_modelPaiCheTabNew.FacConPeoAndTelTabID;
            CurrentUGR.Cells["工厂名称"].Value = m_modelPaiCheTabNew.FactoryDataTabID;
            CurrentUGR.Cells["工厂地址"].Value = m_modelPaiCheTabNew.FacAddrTabID;
            //CurrentUGR.Cells["工厂电话"].Value = "";
            //CurrentUGR.Cells["工厂传真"].Value = "";
            CurrentUGR.Cells["工厂联系人电话"].Value = "";

            CurrentUGR.Cells["柜号"].Value = "";
            CurrentUGR.Cells["封条号"].Value = "";
            CurrentUGR.Cells["到厂小时"].Value = 0;
            CurrentUGR.Cells["到厂分钟"].Value = 0;

            CurrentUGR.Cells["过磅重量"].Value = 0;

            CurrentUGR.Cells["特殊事项"].Value = "";
            CurrentUGR.Cells["备注"].Value = "";
            CurrentUGR.Cells["装柜时长"].Value = 0;
            CurrentUGR.Cells["临时上柜"].Value = false;
            CurrentUGR.Cells["有堆存费"].Value = 0;
            CurrentUGR.Cells["有异提费"].Value = 0;
            CurrentUGR.Cells["有转关费"].Value = 0;
            CurrentUGR.Cells["有运输费"].Value = 0;
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetailCopy(nIndex);

            }

        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllPaiCheTabNew.GetFeeInfo("");
            int n = ds.Tables[0].Rows.Count;
            m_vd = ViewDay.Allday;
            ShowAllRecordInDataGridView("");
        }
        private void InitComBox()
        {
            this.comboBoxShowWay.Items.AddRange(new object[] { "最近半年记录", "全部记录" });
            this.comboBoxShowWay.SelectedIndex = 0;
        }
        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["单据号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["有运输费"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已生成运输费"].Width = 45;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["生成运输费"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["有异提费"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["有转关费"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["有堆存费"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位联系人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司名称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司联系人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["订舱号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["业务员"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜时"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜分"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["柜型"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交柜地点"].Width = 60;   
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜时长"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂地址"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂联系人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂联系人电话"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["柜号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["封条号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜时长"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["到厂小时"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["到厂分钟"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["过磅重量"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装柜时长"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["临时上柜"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["特殊事项"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂信息"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂1名称"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂2名称"].Width = 80;
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
              
                string str = column2.Header.Caption;
                if (str == "订舱号" || str == "装柜日期" || str == "装柜时" || str == "装柜分"
                    /*|| str == "柜号"*/ || str == "封条号" || str == "交柜地点" || str == "业务员"
                    || str == "过磅重量" || str == "装柜时长" || str == "合同号")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                }
            }

            InitColumnWidth();
        }

        private Int64 MakeSJPackDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllPaiCheTabNew.GetSJPackDJNoByDate("PaiCheDJDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }

        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.PaiCheTabNew modelPaiCheTabNew = new DGBY.Model.PaiCheTabNew();
                modelPaiCheTabNew.PaiCheDJNo =  Convert.ToInt64(CurrentUGR.Cells["单据号"].Value.ToString(), 10);
                
                modelPaiCheTabNew.PaiCheDJDate = System.DateTime.Now.Date;
                modelPaiCheTabNew.UserPasswordTabID = Frm_Main.m_nLoginUserId;
                modelPaiCheTabNew.TCCompConPeoTelTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司联系人"].Tag.ToString(), 10);
                modelPaiCheTabNew.TCCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司名称"].Value.ToString(), 10);

                modelPaiCheTabNew.DingCangNo = CurrentUGR.Cells["订舱号"].Value.ToString();
                modelPaiCheTabNew.ZGDate = (DateTime)CurrentUGR.Cells["装柜日期"].Value;
                modelPaiCheTabNew.ZGHour = Convert.ToInt32(CurrentUGR.Cells["装柜时"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGMin = Convert.ToInt32(CurrentUGR.Cells["装柜分"].Value.ToString(), 10);

                modelPaiCheTabNew.GuiXingTabID = Convert.ToInt32(CurrentUGR.Cells["柜型"].Value.ToString(), 10);
                modelPaiCheTabNew.PortTabID = Convert.ToInt32(CurrentUGR.Cells["交柜地点"].Value.ToString(), 10);
                modelPaiCheTabNew.FacAddrTabID = Convert.ToInt32(CurrentUGR.Cells["工厂地址"].Tag.ToString(), 10);
                modelPaiCheTabNew.FacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["工厂联系人"].Tag.ToString(), 10);
                modelPaiCheTabNew.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂名称"].Value.ToString(), 10);
                modelPaiCheTabNew.UserPasswordTabID2 = Convert.ToInt32(CurrentUGR.Cells["业务员"].Value.ToString(), 10);


                modelPaiCheTabNew.GuiNo = CurrentUGR.Cells["柜号"].Value.ToString();
                modelPaiCheTabNew.FengTiaoNo = CurrentUGR.Cells["封条号"].Value.ToString();
                modelPaiCheTabNew.ArriveFacHour = Convert.ToInt32(CurrentUGR.Cells["到厂小时"].Value.ToString(), 10);
                modelPaiCheTabNew.ArriveFacMin = Convert.ToInt32(CurrentUGR.Cells["到厂分钟"].Value.ToString(), 10);
                modelPaiCheTabNew.GuoBangWeight = Convert.ToInt32(CurrentUGR.Cells["过磅重量"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGHourCount = Convert.ToInt32(CurrentUGR.Cells["装柜时长"].Value.ToString(), 10);
                modelPaiCheTabNew.LinShiShangGui = Convert.ToBoolean(CurrentUGR.Cells["临时上柜"].Value.ToString());
                modelPaiCheTabNew.SpecialNote = CurrentUGR.Cells["特殊事项"].Value.ToString();
                modelPaiCheTabNew.Note = CurrentUGR.Cells["备注"].Value.ToString();


                modelPaiCheTabNew.IsGaidanFee = Convert.ToBoolean(CurrentUGR.Cells["有堆存费"].Value.ToString());
                modelPaiCheTabNew.IsYiTiFee = Convert.ToBoolean(CurrentUGR.Cells["有异提费"].Value.ToString());
                modelPaiCheTabNew.IsZhuanGuanFee = Convert.ToBoolean(CurrentUGR.Cells["有转关费"].Value.ToString());
                modelPaiCheTabNew.bitFiledReserv = Convert.ToBoolean(CurrentUGR.Cells["有运输费"].Value.ToString());
                modelPaiCheTabNew.stringFiledReserv1 = "";
                modelPaiCheTabNew.stringFiledReserv2 = "";
                modelPaiCheTabNew.intFiledReserv1 = 0;
                modelPaiCheTabNew.intFiledReserv2 = 0;
                modelPaiCheTabNew.datetimeFiledReserv1 = System.DateTime.Now.Date;
                modelPaiCheTabNew.datetimeFiledReserv2 = System.DateTime.Now.Date;
                modelPaiCheTabNew.bitFiledReserv = false;
                m_bllPaiCheTabNew.Add(modelPaiCheTabNew);
                //if (modelPaiCheTabNew.ArriveFacHour == 0)
                //    CurrentUGR.Cells["到厂小时"].Value = null;
                //if (modelPaiCheTabNew.ArriveFacMin == 0)
                //    CurrentUGR.Cells["到厂分钟"].Value = null;
                //if (modelPaiCheTabNew.GuoBangWeight == 0)
                //    CurrentUGR.Cells["过磅重量"].Value = null;
                //if (modelPaiCheTabNew.ZGHourCount == 0)
                //    CurrentUGR.Cells["装柜时长"].Value = null;

                Frm_Main.Log2DB(this.Text, "增加", modelPaiCheTabNew.PaiCheDJNo.ToString());
                ShowAllRecordInDataGridView("");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool IsNumTest(string strTest)
        {
            try
            {
                int nRet = Convert.ToInt32(strTest, 10);
                return true;

            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.PaiCheTabNew modelPaiCheTabNew = m_bllPaiCheTabNew.GetModel(nSelectID);


                //modelPaiCheTabNew.PaiCheDJNo = Convert.ToInt64(CurrentUGR.Cells["单据号"].Value.ToString(), 10);

                //modelPaiCheTabNew.PaiCheDJDate = System.DateTime.Now.Date;
                modelPaiCheTabNew.UserPasswordTabID = Frm_Main.m_nLoginUserId;
                if (CurrentUGR.Cells["拖车公司联系人"].Tag != null && IsNumTest(CurrentUGR.Cells["拖车公司联系人"].Tag.ToString()))
                {
                    modelPaiCheTabNew.TCCompConPeoTelTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司联系人"].Tag.ToString(), 10);
                }
                else if (IsNumTest(CurrentUGR.Cells["拖车公司联系人"].Value.ToString()))
                {
                    modelPaiCheTabNew.TCCompConPeoTelTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司联系人"].Value.ToString(), 10);
                }
                
                if (IsNumTest(CurrentUGR.Cells["拖车公司名称"].Value.ToString()))
                    modelPaiCheTabNew.TCCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["拖车公司名称"].Value.ToString(), 10);

                //有运输费
                modelPaiCheTabNew.bitFiledReserv = Convert.ToBoolean(CurrentUGR.Cells["有运输费"].Value.ToString());

                modelPaiCheTabNew.DingCangNo = CurrentUGR.Cells["订舱号"].Value.ToString();
                modelPaiCheTabNew.ZGDate = (DateTime)CurrentUGR.Cells["装柜日期"].Value;
                modelPaiCheTabNew.ZGHour = Convert.ToInt32(CurrentUGR.Cells["装柜时"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGMin = Convert.ToInt32(CurrentUGR.Cells["装柜分"].Value.ToString(), 10);

                if (IsNumTest(CurrentUGR.Cells["柜型"].Value.ToString()))
                    modelPaiCheTabNew.GuiXingTabID = Convert.ToInt32(CurrentUGR.Cells["柜型"].Value.ToString(), 10);



                if (IsNumTest(CurrentUGR.Cells["交柜地点"].Value.ToString()))
                    modelPaiCheTabNew.PortTabID = Convert.ToInt32(CurrentUGR.Cells["交柜地点"].Value.ToString(), 10);
                //if (IsNumTest(CurrentUGR.Cells["业务员"].Value.ToString()))
                //    modelPaiCheTabNew.ArriveFacHour = Convert.ToInt32(CurrentUGR.Cells["业务员"].Value.ToString(), 10);

                if (CurrentUGR.Cells["工厂地址"].Tag != null && IsNumTest(CurrentUGR.Cells["工厂地址"].Tag.ToString()))
                {
                    modelPaiCheTabNew.FacAddrTabID = Convert.ToInt32(CurrentUGR.Cells["工厂地址"].Tag.ToString(), 10);
                }
                else if (IsNumTest(CurrentUGR.Cells["工厂地址"].Value.ToString()))
                {
                    modelPaiCheTabNew.FacAddrTabID = Convert.ToInt32(CurrentUGR.Cells["工厂地址"].Value.ToString(), 10);
                }

                if (CurrentUGR.Cells["工厂联系人"].Tag != null && IsNumTest(CurrentUGR.Cells["工厂联系人"].Tag.ToString()))
                {
                    modelPaiCheTabNew.FacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["工厂联系人"].Tag.ToString(), 10);
                }
                else if (IsNumTest(CurrentUGR.Cells["工厂联系人"].Value.ToString()))
                {
                    modelPaiCheTabNew.FacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["工厂联系人"].Value.ToString(), 10);
                } 
         
                    
                if (IsNumTest(CurrentUGR.Cells["工厂名称"].Value.ToString()))
                    modelPaiCheTabNew.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂名称"].Value.ToString(), 10);

                modelPaiCheTabNew.IsGaidanFee = Convert.ToBoolean(CurrentUGR.Cells["有堆存费"].Value.ToString());
                modelPaiCheTabNew.IsYiTiFee = Convert.ToBoolean(CurrentUGR.Cells["有异提费"].Value.ToString());
                modelPaiCheTabNew.IsZhuanGuanFee = Convert.ToBoolean(CurrentUGR.Cells["有转关费"].Value.ToString());

                modelPaiCheTabNew.GuiNo = CurrentUGR.Cells["柜号"].Value.ToString();

                if (!string.IsNullOrEmpty(modelPaiCheTabNew.GuiNo))
                {
                    int nGuiXing = GeteBGPackRecGuiXingID(modelPaiCheTabNew.GuiNo);
                    if (nGuiXing > 0 && nGuiXing != modelPaiCheTabNew.GuiXingTabID)
                    {
                        MessageBox.Show("柜型与报关装箱单设置不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                modelPaiCheTabNew.FengTiaoNo = CurrentUGR.Cells["封条号"].Value.ToString();
                modelPaiCheTabNew.ArriveFacHour = Convert.ToInt32(CurrentUGR.Cells["到厂小时"].Value.ToString() == "" ? "0" : CurrentUGR.Cells["到厂小时"].Value.ToString(), 10);
                modelPaiCheTabNew.ArriveFacMin = Convert.ToInt32(CurrentUGR.Cells["到厂分钟"].Value.ToString() == "" ? "0" : CurrentUGR.Cells["到厂分钟"].Value.ToString(), 10);
                modelPaiCheTabNew.GuoBangWeight = Convert.ToInt32(CurrentUGR.Cells["过磅重量"].Value.ToString() == "" ? "0" : CurrentUGR.Cells["过磅重量"].Value.ToString(), 10);
                modelPaiCheTabNew.ZGHourCount = Convert.ToInt32(CurrentUGR.Cells["装柜时长"].Value.ToString() == "" ? "0" : CurrentUGR.Cells["装柜时长"].Value.ToString(), 10);
                modelPaiCheTabNew.LinShiShangGui = Convert.ToBoolean(CurrentUGR.Cells["临时上柜"].Value.ToString());
                modelPaiCheTabNew.SpecialNote = CurrentUGR.Cells["特殊事项"].Value.ToString();
                modelPaiCheTabNew.Note = CurrentUGR.Cells["备注"].Value.ToString();
                if (IsNumTest(CurrentUGR.Cells["业务员"].Value.ToString()))
                    modelPaiCheTabNew.UserPasswordTabID2 = Convert.ToInt32(CurrentUGR.Cells["业务员"].Value.ToString() == "" ? "0" : CurrentUGR.Cells["业务员"].Value.ToString(), 10);
                else
                {
                    DataSet ds = m_bllT_ACL_User.GetList("T_ACL_User.Name = '" + CurrentUGR.Cells["业务员"].Value.ToString() + "'");
                    modelPaiCheTabNew.UserPasswordTabID2 = (int)ds.Tables[0].Rows[0]["UserPasswordTabID"];
                }
                m_bllPaiCheTabNew.Update(modelPaiCheTabNew);
               // ShowAllRecordInDataGridView("");
                Frm_Main.Log2DB(this.Text, "修改", modelPaiCheTabNew.PaiCheDJNo.ToString());

                return true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private int GeteBGPackRecGuiXingID(string strGuiNo)
        {
            return m_bllBGPackingTab.GeteBGGuiXingIDByGuiNo(strGuiNo);
        }

        private void toolStripMenuItemCopyAdd_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            if (m_bCopyInsertFlag == false)
                InitNewAddRowValue(e.Row.Index);
            else
            {
                InitNewAddRowValueCopyContract(e.Row.Index);
                m_bCopyInsertFlag = false;
            }
        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            int nIndex = e.Cell.Row.Index;
            if (nIndex < 0)
                return;
            if (e.Cell.Column.Key == "工厂名称")
            {
              
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                string strID = e.Cell.Value.ToString();


                DataSet ds = m_bllFacAddrTab.GetFacInfo("FacAddrTab.FactoryDataTabID = '" + strID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    CurrentUGR.Cells["工厂地址"].Value = ds.Tables[0].Rows[0]["工厂地址"].ToString();
                    CurrentUGR.Cells["工厂地址"].Tag = ds.Tables[0].Rows[0]["FacAddrTabID"].ToString();
                    CurrentUGR.Cells["工厂联系人"].Value = ds.Tables[0].Rows[0]["工厂联系人"].ToString();
                    CurrentUGR.Cells["工厂联系人"].Tag = ds.Tables[0].Rows[0]["FacConPeoAndTelTabID"].ToString();
                    CurrentUGR.Cells["工厂电话"].Value = ds.Tables[0].Rows[0]["工厂电话"].ToString();
                    CurrentUGR.Cells["工厂传真"].Value = ds.Tables[0].Rows[0]["工厂传真"].ToString();
                    CurrentUGR.Cells["工厂联系人电话"].Value = ds.Tables[0].Rows[0]["工厂联系人电话"].ToString();

                }

            }
            if (e.Cell.Column.Key == "工厂联系人")
            {

                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                string strID = e.Cell.Value.ToString();
                DataSet ds;
                if (ClassInputValid.IsNum(strID))
                {
                    ds = m_bllFacConPeoAndTelTab.GetList("FacConPeoAndTelTab.FacConPeoAndTelTabID = '" + strID + "'");
                }
                else
                {
                    ds = m_bllFacConPeoAndTelTab.GetList("FacConPeoAndTelTab.FacConPeoName = '" + strID + "'");

                }
                
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {


                    CurrentUGR.Cells["工厂联系人电话"].Value = ds.Tables[0].Rows[0]["FacConPeoTel"].ToString();

                }

            }
            if (e.Cell.Column.Key == "拖车公司名称")
            {
                
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                string strID = e.Cell.Value.ToString();


                DataSet ds = m_bllTCCompConPeoTelTab.GetInfoList("TCCompConPeoTelTab.TCCompDataTabID = '" + strID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    CurrentUGR.Cells["拖车公司联系人"].Value = ds.Tables[0].Rows[0]["TCCompConPeoName"].ToString();
                    CurrentUGR.Cells["拖车公司联系人"].Tag = ds.Tables[0].Rows[0]["TCCompConPeoTelTabID"].ToString();
                    CurrentUGR.Cells["拖车公司电话"].Value = ds.Tables[0].Rows[0]["TCCompTel"].ToString();
                    CurrentUGR.Cells["拖车公司传真"].Value = ds.Tables[0].Rows[0]["TCCompFax"].ToString();
                }

            } 
        }

        private void ultraGrid1_CellChange(object sender, CellEventArgs e)
        {
           
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {
                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "拖车公司名称" 
                            || strTemp == "拖车公司联系人" 
                            || strTemp == "工厂名称"
                            || strTemp == "工厂地址"
                            || strTemp == "工厂联系人" 
                            || strTemp == "交柜地点"
                            || strTemp == "柜型"
                            || strTemp == "业务员")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "拖车公司名称"
                             || strTemp == "拖车公司联系人"
                             || strTemp == "工厂名称"
                             || strTemp == "工厂地址"
                             || strTemp == "工厂联系人"
                             || strTemp == "交柜地点"
                             || strTemp == "柜型"
                             || strTemp == "业务员")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {
                    DialogResult dr = MessageBox.Show("是否要修改记录" + (e.Row.Index + 1).ToString(), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                        ModifyGoodsDetail(e.Row.Index);
                    else
                        return;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
             e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.PaiCheTabNew modelPaiCheTabNewOld = m_bllPaiCheTabNew.GetModel(nSelectID);

                if (modelPaiCheTabNewOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllPaiCheTabNew.Delete(nSelectID);

                        Frm_Main.Log2DB(this.Text, "删除", modelPaiCheTabNewOld.PaiCheDJNo.ToString());
                      
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        
        }

        private void ultraGrid1_AfterRowCancelUpdate(object sender, RowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            bool bVisiable = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司电话"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司传真"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂电话"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂传真"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂地址"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂信息"].Hidden = bVisiable;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["生成运输费"].Hidden = bVisiable;
            
            DataTable dt = (DataTable)ultraGrid1.DataSource;
            
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
            bVisiable = false;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司电话"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拖车公司传真"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂电话"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂传真"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂地址"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂联系人"].Hidden = bVisiable;
            ultraGrid1.DisplayLayout.Bands[0].Columns["拼柜工厂信息"].Hidden = bVisiable;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["生成运输费"].Hidden = bVisiable;

        }

        private void 复制增加多行ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelPaiCheTabNew = m_bllPaiCheTabNew.GetModel(nSelectID);

                    m_bCopyInsertFlag = true;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();

                }
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }

        private void 删除一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                if (ultraGrid1.ActiveRow != null)
                {
                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
         
            // string strColumnName = e.GridColumn;
            if (e.GridColumn.Key == "单据号")
            {
                int iRdex = e.CurrentRowIndex;
                int iCdex = e.CurrentColumnIndex;
                if (e.Value != null)
                {
                    string stringTemp = e.Value.ToString();
                    ws.Rows[iRdex].Cells[iCdex].Value = stringTemp;
                }
            }
            else if (e.GridColumn.Key == "临时上柜")
            {
                int iRdex = e.CurrentRowIndex;
                int iCdex = e.CurrentColumnIndex;
                string stringTemp = "N";
                if (e.Value != null)
                {
                    if((bool)e.Value == true)
                        stringTemp = "Y";
                    ws.Rows[iRdex].Cells[iCdex].Value = stringTemp;
                }
            }

        }

        private void ultraGridExcelExporter1_CellExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventArgs e)
        {

        }

        private void ultraGrid1_ClickCellButton(object sender, CellEventArgs e)
        {
            int nIndex = e.Cell.Row.Index;
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
            int nSelectID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
            DGBY.Model.PaiCheTabNew modelPaiCheTabNew = m_bllPaiCheTabNew.GetModel(nSelectID);

            string strTemp = e.Cell.Column.Header.Caption;
            if (strTemp == "生成运输费")
            {
                /*
                int nFacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["FacConPeoAndTelTabID"].Value.ToString(), 10);
                DataSet ds = m_bllBGPackingTab.GetPaicheRecvFeeInfo("PaidanRegisterTab.FacConPeoAndTelTabID ='" + nFacConPeoAndTelTabID.ToString() + "'"
                                            + " and GuiNo ='" + CurrentUGR.Cells["柜号"].Value.ToString() + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataSet dsInfo = m_bllPaiCheTabNew.GetFeeInfo("PaiCheTabNew.FacConPeoAndTelTabID ='" + nFacConPeoAndTelTabID.ToString() + "'"
                                            + " and PaiCheTabNew.GuiNo ='" + CurrentUGR.Cells["柜号"].Value.ToString() + "'");
                    if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
                    {

                        DGBY.Model.FeeDLYSTruePayTab modelFeeDLYSTruePayTab = new DGBY.Model.FeeDLYSTruePayTab();
                        modelFeeDLYSTruePayTab.PaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                        modelFeeDLYSTruePayTab.FactoryDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                        modelFeeDLYSTruePayTab.TCCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["TCCompDataTabID"].Value.ToString(), 10);
                        modelFeeDLYSTruePayTab.ZhuangGuiSiteTabID = Convert.ToInt32(CurrentUGR.Cells["TCCompDataTabID"].Value.ToString(), 10);
                        modelFeeDLYSTruePayTab.UserPasswordTabID = Convert.ToInt32(CurrentUGR.Cells["UserPasswordTabID2"].Value.ToString(), 10);


                        modelFeeDLYSTruePayTab.YSFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付运输费定价"];
                        modelFeeDLYSTruePayTab.YTFee = modelPaiCheTabNew.IsYiTiFee ? 100 : 0;
                        modelFeeDLYSTruePayTab.DDFee = modelPaiCheTabNew.ZGHour == -2 ? 30 : 0;
                        modelFeeDLYSTruePayTab.CZFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付超重费定价"];
                        modelFeeDLYSTruePayTab.YCFee = 0;
                        modelFeeDLYSTruePayTab.GSFee = 0;

                        modelFeeDLYSTruePayTab.GBFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付过磅费定价"];
                        modelFeeDLYSTruePayTab.PGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付拼柜费定价"];
                        modelFeeDLYSTruePayTab.DCFee = modelPaiCheTabNew.ZGHour == -1 ? 250 : 0;
                        modelFeeDLYSTruePayTab.DGFee = 0;
                        modelFeeDLYSTruePayTab.FTFee = 0;
                        modelFeeDLYSTruePayTab.FKFee = modelPaiCheTabNew.ZGHour == -3 ? modelFeeDLYSTruePayTab.YSFee * 4 / 5 : 0;
                        modelFeeDLYSTruePayTab.GDFee = modelPaiCheTabNew.IsGaidanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.ZGFee = modelPaiCheTabNew.IsZhuanGuanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.OtherFee = 0;


                        modelFeeDLYSTruePayTab.YSYSFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收运输费定价"];
                        modelFeeDLYSTruePayTab.YSYTFee = modelPaiCheTabNew.IsYiTiFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSDDFee = modelPaiCheTabNew.ZGHour == -2 ? 40 : 0;
                        modelFeeDLYSTruePayTab.YSCZFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收超重费定价"];
                        modelFeeDLYSTruePayTab.YSYCFee = 0;
                        modelFeeDLYSTruePayTab.YSGSFee = 0;

                        modelFeeDLYSTruePayTab.YSGBFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收过磅费定价"];
                        modelFeeDLYSTruePayTab.YSPGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收拼柜费定价"];
                        modelFeeDLYSTruePayTab.YSDCFee = modelPaiCheTabNew.ZGHour == -1 ? 250 : 0;
                        modelFeeDLYSTruePayTab.YSDGFee = 0;
                        modelFeeDLYSTruePayTab.YSFTFee = 0;
                        modelFeeDLYSTruePayTab.YSFKFee = modelPaiCheTabNew.ZGHour == -3 ? modelFeeDLYSTruePayTab.YSFee * 4 / 5 : 0;
                        modelFeeDLYSTruePayTab.YSGDFee = modelPaiCheTabNew.IsGaidanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSZGFee = modelPaiCheTabNew.IsZhuanGuanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSOtherFee = 0;

                        modelFeeDLYSTruePayTab.ToPubBianHao = "";
                        modelFeeDLYSTruePayTab.ToPrivateBianHao = "";

                        //modelFeeDLYSTruePayTab.InputDate = DateTime.Parse("1900-01-01");
                        //modelFeeDLYSTruePayTab.TruePayDate = DateTime.Parse("1900-01-01");
                        modelFeeDLYSTruePayTab.SBDate = DateTime.Parse("1900-01-01");

                        modelFeeDLYSTruePayTab.TruePayMonth = 0;
                        modelFeeDLYSTruePayTab.ShouldPayMonth = 0;
                        modelFeeDLYSTruePayTab.PubTruePayMoney = 0;
                        modelFeeDLYSTruePayTab.PrivateTruePayMoney = 0;
                        modelFeeDLYSTruePayTab.ZGDate = (DateTime)CurrentUGR.Cells["装柜日期"].Value;


                        modelFeeDLYSTruePayTab.JinBanPerson = "";
                        modelFeeDLYSTruePayTab.RecvMonyePerson = "";
                        modelFeeDLYSTruePayTab.IsToRecvPayTab = 0;
                        modelFeeDLYSTruePayTab.IsToFinCheckMoneyTab = 0;

                        modelFeeDLYSTruePayTab.Note = "";

                        modelFeeDLYSTruePayTab.bitFiledReserv1 = false;
                        modelFeeDLYSTruePayTab.bitFiledReserv2 = false;

                        modelFeeDLYSTruePayTab.stringFiledReserv1 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv2 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv3 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv4 = "";

                        modelFeeDLYSTruePayTab.intFiledReserv1 = 0;
                        modelFeeDLYSTruePayTab.intFiledReserv2 = 0;
                        modelFeeDLYSTruePayTab.intFiledReserv3 = 0;

                        modelFeeDLYSTruePayTab.datetimeFiledReserv1 = System.DateTime.Now.Date;
                        modelFeeDLYSTruePayTab.datetimeFiledReserv2 = System.DateTime.Now.Date;

                        modelFeeDLYSTruePayTab.decimalFiledReserv1 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv2 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv3 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv4 = 0;
                        
                        modelFeeDLYSTruePayTab.ContractNo = ds.Tables[0].Rows[0]["合同编号"].ToString();

                        int nID = m_bllFeeDLYSTruePayTab.Add(modelFeeDLYSTruePayTab);
                        Frm_FeeDLYSTruePayTab f = new Frm_FeeDLYSTruePayTab(nID);

                        Frm_Main.GetMainForm().ShowContent(f);
                        CurrentUGR.Cells["是否已收费"].Value = 1;
                        modelPaiCheTabNew.bitFiledReserv = true;
                        m_bllPaiCheTabNew.Update(modelPaiCheTabNew);

                        Frm_Main.Log2DB(this.Text, "生成运输费", modelPaiCheTabNew.PaiCheDJNo.ToString());
                    }
                    else
                    {
                        MessageBox.Show("没有找到柜号为" + CurrentUGR.Cells["柜号"].Value.ToString() + "  工厂为"
                       + CurrentUGR.Cells["工厂名称"].Value.ToString() + "的合同记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    
                }
                else
                {
                    MessageBox.Show("没有找到柜号为" + CurrentUGR.Cells["柜号"].Value.ToString() +　"  工厂为"
                        + CurrentUGR.Cells["工厂名称"].Value.ToString() + "的合同记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                */
            }
            else
            {

                

                Frm_PaicheAddFac frm_PaicheAddFac = new Frm_PaicheAddFac(nSelectID);
                frm_PaicheAddFac.StartPosition = FormStartPosition.CenterScreen;
                frm_PaicheAddFac.ShowDialog();
                DataSet ds = m_bllPaiCheTabNew.GetList("PaiCheTabNew.PaiCheTabNewID = '" + nSelectID + "'");
                CurrentUGR.Cells["拼柜工厂1名称"].Value = ds.Tables[0].Rows[0]["拼柜工厂1名称"].ToString();
                CurrentUGR.Cells["拼柜工厂2名称"].Value = ds.Tables[0].Rows[0]["拼柜工厂2名称"].ToString();
            }
           // ultraGrid1.Rows[nIndex].Refresh(RefreshRow.ReloadData);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<int, DataTable> dicPaiCheTabNewID2FacList = new Dictionary<int, DataTable>();
             
            UltraGridRow CurrentUGR;
            CurrentUGR = ultraGrid1.Rows[m_nDelIndex];
            string strDanJuNo = CurrentUGR.Cells["单据号"].Value.ToString();
            DataSet ds = m_bllPaiCheTabNew.GetList("PaiCheDJNo = '" + strDanJuNo + "'");
            DataTable dt = ds.Tables[0];
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nID = (int)ds.Tables[0].Rows[i]["PaiCheTabNewID"];
                    DataSet dsTemp = m_bllPaiChe_FacTab.GetFacInfo("PaiCheTabNewID ='" + nID + "'");
                    if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                    {
                        dicPaiCheTabNewID2FacList.Add(nID, dsTemp.Tables[0]);
                    }
                }
            }
            if (dt == null)
            {
                MessageBox.Show("派车单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    //SaveFileDialog sfd = new SaveFileDialog();
                    //sfd.Filter = "Excel文件(*.xls)|*.xls";
                    //string outFile = "";

                    //if (sfd.ShowDialog() == DialogResult.OK)
                    //{
                    //    outFile = sfd.FileName;
                    //}
                    //else
                    //{
                    //    return;
                    //}
                    string outFile = "";
                    string strFac = dt.Rows[0]["工厂名称"].ToString();
                    string strTCComp = dt.Rows[0]["拖车公司名称"].ToString();
                    string strDate = ((System.DateTime)dt.Rows[0]["装柜日期"]).ToShortDateString();
                    
                    
                    string strPre = strFac + "-" + strTCComp + "-" + strDate;

                    string strParentFolder = m_strFolder + strFac + "/";
                    if (!Directory.Exists(strParentFolder))
                    {
                        Directory.CreateDirectory(strParentFolder);
                    }
                    for (int i = 1; i < 1000; i++)
                    {
                        outFile = strParentFolder + strPre + "-" + i.ToString().PadLeft(3, '0') + ".xls";
                        if (File.Exists(outFile))
                            continue;
                        else
                            break;
                    }
                    //string outFile = m_strFolder + "广东省发票" + ".xls";
                    string templetFile = templetFilePath + "派车单.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DispachCar_DataTableToExcelNew(dt,dicPaiCheTabNewID2FacList
                            , 10, 14, 1, "页", 2);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_vd = ViewDay.Today;
            ShowAllRecordInDataGridView("");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            m_vd = ViewDay.Yesterday;
            ShowAllRecordInDataGridView("");
        }

        private void buttonTomorrow_Click(object sender, EventArgs e)
        {
            m_vd = ViewDay.Tomorrow;
            ShowAllRecordInDataGridView("");
        }


        private void ultraDropDownFacAddr_BeforeDropDown(object sender, CancelEventArgs e)
        {
            int nIndex = ultraGrid1.ActiveCell.Row.Index;
            UltraGridRow dropDownRow = ultraGrid1.Rows[nIndex];

            string strID = dropDownRow.Cells["FactoryDataTabID"].Value.ToString();

            DataSet dsTemp = m_bllFacAddrTab.GetList("FactoryDataTabID = '" + strID + "'");
            this.ultraDropDownFacAddr.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFacAddr.DisplayMember = "FacAddr";
            this.ultraDropDownFacAddr.ValueMember = "FacAddrTabID";
            this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFacAddr.DisplayLayout.Bands[0].Columns["FacAddr"].Hidden = false;
        }

        private void ultraDropDownFacCeo_BeforeDropDown(object sender, CancelEventArgs e)
        {
            int nIndex = ultraGrid1.ActiveCell.Row.Index;
            UltraGridRow dropDownRow = ultraGrid1.Rows[nIndex];

            string strID = dropDownRow.Cells["FactoryDataTabID"].Value.ToString();

            DataSet dsTemp = m_bllFacConPeoAndTelTab.GetList("FactoryDataTabID = '" + strID + "'");

            this.ultraDropDownFacCeo.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFacCeo.DisplayMember = "FacConPeoName";
            this.ultraDropDownFacCeo.ValueMember = "FacConPeoAndTelTabID";
            this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFacCeo.DisplayLayout.Bands[0].Columns["FacConPeoName"].Hidden = false;
        }

        private void 生成运输费ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要生成运输费么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

                int nSelectID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.PaiCheTabNew modelPaiCheTabNew = m_bllPaiCheTabNew.GetModel(nSelectID);

                int nFacConPeoAndTelTabID = Convert.ToInt32(CurrentUGR.Cells["FacConPeoAndTelTabID"].Value.ToString(), 10);
                DataSet ds = m_bllBGPackingTab.GetPaicheRecvFeeInfo("PaidanRegisterTab.FacConPeoAndTelTabID ='" + nFacConPeoAndTelTabID.ToString() + "'"
                                            + " and GuiNo ='" + CurrentUGR.Cells["柜号"].Value.ToString() + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataSet dsInfo = m_bllPaiCheTabNew.GetFeeInfo("PaiCheTabNew.FacConPeoAndTelTabID ='" + nFacConPeoAndTelTabID.ToString() + "'"
                                            + " and PaiCheTabNew.GuiNo ='" + CurrentUGR.Cells["柜号"].Value.ToString() + "'");
                    if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
                    {

                        DGBY.Model.FeeDLYSTruePayTab modelFeeDLYSTruePayTab = new DGBY.Model.FeeDLYSTruePayTab();
                        modelFeeDLYSTruePayTab.PaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                        modelFeeDLYSTruePayTab.FactoryDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                        modelFeeDLYSTruePayTab.TCCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["TCCompDataTabID"].Value.ToString(), 10);
                        modelFeeDLYSTruePayTab.ZhuangGuiSiteTabID = Convert.ToInt32(CurrentUGR.Cells["TCCompDataTabID"].Value.ToString(), 10);
                        modelFeeDLYSTruePayTab.UserPasswordTabID = Convert.ToInt32(CurrentUGR.Cells["UserPasswordTabID2"].Value.ToString(), 10);

                        if (modelPaiCheTabNew.bitFiledReserv)
                            modelFeeDLYSTruePayTab.YSFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付运输费定价"];
                        else
                            modelFeeDLYSTruePayTab.YSFee = 0;
                        
                        modelFeeDLYSTruePayTab.YTFee = modelPaiCheTabNew.IsYiTiFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.DDFee = modelPaiCheTabNew.ZGHour == -2 ? 30 : 0;
                        modelFeeDLYSTruePayTab.CZFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付超重费定价"];
                        modelFeeDLYSTruePayTab.YCFee = 0;
                        modelFeeDLYSTruePayTab.GSFee = 0;

                        //modelFeeDLYSTruePayTab.GBFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付过磅费定价"];

                        if (modelPaiCheTabNew.GuoBangWeight == 0)
                            modelFeeDLYSTruePayTab.GBFee = 0;
                        else if (modelPaiCheTabNew.GuoBangWeight > 0 && modelPaiCheTabNew.GuoBangWeight <= 23)
                            modelFeeDLYSTruePayTab.GBFee = 100;
                        else if (modelPaiCheTabNew.GuoBangWeight >23 && modelPaiCheTabNew.GuoBangWeight <= 25)
                            modelFeeDLYSTruePayTab.GBFee = 200;
                        else if (modelPaiCheTabNew.GuoBangWeight >25)
                            modelFeeDLYSTruePayTab.GBFee = 300;

                        DataSet dsPG = m_bllPaiChe_FacTab.GetFacInfo("PaiCheTabNewID ='" + modelPaiCheTabNew.PaiCheTabNewID + "'");
                        if (dsPG.Tables.Count > 0)
                        {
                            int nFacCount = dsPG.Tables[0].Rows.Count;
                            modelFeeDLYSTruePayTab.PGFee = nFacCount * 150;
                        }

                        modelFeeDLYSTruePayTab.DCFee = modelPaiCheTabNew.IsGaidanFee ? 250 : 0;
                        modelFeeDLYSTruePayTab.DGFee = 0;
                        modelFeeDLYSTruePayTab.FTFee = 0;
                        modelFeeDLYSTruePayTab.FKFee = modelPaiCheTabNew.ZGHour == -3 ? modelFeeDLYSTruePayTab.YSFee * 4 / 5 : 0;
                        modelFeeDLYSTruePayTab.GDFee = 0;
                        modelFeeDLYSTruePayTab.ZGFee = modelPaiCheTabNew.IsZhuanGuanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.OtherFee = 0;

/*
                        modelFeeDLYSTruePayTab.YSYSFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收运输费定价"];
                        modelFeeDLYSTruePayTab.YSYTFee = modelPaiCheTabNew.IsYiTiFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSDDFee = modelPaiCheTabNew.ZGHour == -2 ? 40 : 0;
                        modelFeeDLYSTruePayTab.YSCZFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收超重费定价"];
                        modelFeeDLYSTruePayTab.YSYCFee = 0;
                        modelFeeDLYSTruePayTab.YSGSFee = 0;

                        modelFeeDLYSTruePayTab.YSGBFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收过磅费定价"];
                        modelFeeDLYSTruePayTab.YSPGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收拼柜费定价"];
                        modelFeeDLYSTruePayTab.YSDCFee = modelPaiCheTabNew.ZGHour == -1 ? 250 : 0;
                        modelFeeDLYSTruePayTab.YSDGFee = 0;
                        modelFeeDLYSTruePayTab.YSFTFee = 0;
                        modelFeeDLYSTruePayTab.YSFKFee = modelPaiCheTabNew.ZGHour == -3 ? modelFeeDLYSTruePayTab.YSFee * 4 / 5 : 0;
                        modelFeeDLYSTruePayTab.YSGDFee = modelPaiCheTabNew.IsGaidanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSZGFee = modelPaiCheTabNew.IsZhuanGuanFee ? 150 : 0;
                        modelFeeDLYSTruePayTab.YSOtherFee = 0;
*/

                        modelFeeDLYSTruePayTab.ToPubBianHao = "";
                        modelFeeDLYSTruePayTab.ToPrivateBianHao = "";

                        //modelFeeDLYSTruePayTab.InputDate = DateTime.Parse("1900-01-01");
                        //modelFeeDLYSTruePayTab.TruePayDate = DateTime.Parse("1900-01-01");
                        modelFeeDLYSTruePayTab.SBDate = DateTime.Parse("1900-01-01");

                        modelFeeDLYSTruePayTab.TruePayMonth = 0;
                        modelFeeDLYSTruePayTab.ShouldPayMonth = 0;
                        modelFeeDLYSTruePayTab.PubTruePayMoney = 0;
                        modelFeeDLYSTruePayTab.PrivateTruePayMoney = 0;
                        modelFeeDLYSTruePayTab.ZGDate = (DateTime)CurrentUGR.Cells["装柜日期"].Value;


                        modelFeeDLYSTruePayTab.JinBanPerson = "";
                        modelFeeDLYSTruePayTab.RecvMonyePerson = "";
                        modelFeeDLYSTruePayTab.IsToRecvPayTab = 0;
                        modelFeeDLYSTruePayTab.IsToFinCheckMoneyTab = 0;

                        modelFeeDLYSTruePayTab.Note = "";

                        modelFeeDLYSTruePayTab.bitFiledReserv1 = false;
                        modelFeeDLYSTruePayTab.bitFiledReserv2 = false;

                        modelFeeDLYSTruePayTab.stringFiledReserv1 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv2 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv3 = "";
                        modelFeeDLYSTruePayTab.stringFiledReserv4 = "";

                        modelFeeDLYSTruePayTab.intFiledReserv1 = 0;
                        modelFeeDLYSTruePayTab.intFiledReserv2 = 0;
                        modelFeeDLYSTruePayTab.intFiledReserv3 = 0;

                        modelFeeDLYSTruePayTab.datetimeFiledReserv1 = System.DateTime.Now.Date;
                        modelFeeDLYSTruePayTab.datetimeFiledReserv2 = System.DateTime.Now.Date;

                        modelFeeDLYSTruePayTab.decimalFiledReserv1 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv2 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv3 = 0;
                        modelFeeDLYSTruePayTab.decimalFiledReserv4 = 0;

                        modelFeeDLYSTruePayTab.ContractNo = ds.Tables[0].Rows[0]["合同编号"].ToString();

                        int nID = m_bllFeeDLYSTruePayTab.Add(modelFeeDLYSTruePayTab);
                        Frm_FeeDLYSTruePayTab f = new Frm_FeeDLYSTruePayTab(nID);

                        Frm_Main.GetMainForm().ShowContent(f);
                        CurrentUGR.Cells["有运输费"].Value = 1;
                        m_bllPaiCheTabNew.Update(modelPaiCheTabNew);

                        CurrentUGR.Cells["是否已生成运输费"].Value = "是";
                        Frm_Main.Log2DB(this.Text, "生成运输费", modelPaiCheTabNew.PaiCheDJNo.ToString());
                    }
                    else
                    {
                        MessageBox.Show("没有找到柜号为" + CurrentUGR.Cells["柜号"].Value.ToString() + "  工厂为"
                       + CurrentUGR.Cells["工厂名称"].Value.ToString() + "的合同记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中要生成运输费的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
